<template>
  <div class="mod-card__dealer">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
      @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column prop="id" :label="$t('gate.modeId')" header-align="center" align="center"></el-table-column>
      <el-table-column prop="name" :label="$t('gate.modeName')" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" :label="$t('gate.modeStatus')" header-align="center" align="center">
        <template v-slot="scope">
          <el-switch v-model="scope.row.status" inline-prompt :active-value="1" :inactive-value="0" active-text="启用"
            inactive-text="关闭" @change="handleSwitch($event, scope.row.id, scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column prop="createDate" :label="$t('gate.createDate')" header-align="center" align="center">
        <template v-slot="scope">
          <span>{{ dayjs(scope.row.createDate).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateDate" :label="$t('gate.updateDate')" header-align="center" align="center">
        <template v-slot="scope">
          <span>{{ dayjs(scope.row.updateDate).format('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row)">{{ $t("update") }}</el-button>
          <el-button type="danger" link @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
      :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
      @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import dayjs from 'dayjs';
import AddOrUpdate from "./mode-add-or-update.vue";
import { reactive, ref, toRefs } from "vue";
import { ModeStatusNames } from './contant';
import { useI18n } from "vue-i18n";
import { ElMessage, ElMessageBox } from "element-plus";

const { t } = useI18n();
const view = reactive({
  getDataListURL: "/gate/mode/query",
  getDataListIsPage: false,
  deleteURL: "/gate/record",
  deleteIsBatch: true,
  dataForm: {}
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (row?: any) => {
  addOrUpdateRef.value.init(row);
};

const deleteHandle = (id: string) => {
  ElMessageBox.confirm(t("prompt.info", { handle: t("delete") }), t("prompt.title"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning"
  })
    .then(() => {
      baseService.get('/gate/mode/remove', { id }).then((res) => {
        ElMessage.success({
          message: t("prompt.success"),
          duration: 500,
          onClose: () => {
            state.getDataList();
          }
        });
      })
    })
}

const handleSwitch = (e: boolean, modeId: string, index: number) => {
  if (state.dataList) {
    state.dataList[index].status = e ? 0 : 1;
  }
  ElMessageBox.confirm(t("prompt.info", { handle: e ? '开启' : '关闭' }), t("prompt.title"), {
    confirmButtonText: t("confirm"),
    cancelButtonText: t("cancel"),
    type: "warning"
  })
    .then(() => {
      baseService.get(`/gate/mode/${e ? 'start' : 'stop'}/rule`, { modeId }).then((res) => {
        ElMessage.success({
          message: t("prompt.success"),
          duration: 500,
          onClose: () => {
            state.getDataList();
          }
        });
      })
    })
}
</script>
